<?php

namespace app\api\controller;

use app\api\model\ExamLog;
use app\common\controller\Api;
use app\api\model\Subject as SubjectModel;
use think\Config;

class Subject extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    protected $num = 20;

    public function subjectList()
    {
        $user = $this->auth;
        $SubjectModel = new SubjectModel();
        $where['category_id'] = $user->identity;
        $list = $SubjectModel->where($where)->orderRaw('RAND()')->limit($this->num)->select();
        foreach ($list as &$v) {
            $type = 1;
            $answer = explode(',', $v['answer']);
            if (count($answer) > 1) {
                $type = 2;
            }
            $option_arr=json_decode($v['option_json'], true);
            foreach ($option_arr as $k=>$vv) {
                $option_arrse[$k]['option']=$k;
                $option_arrse[$k]['title']=$vv;
            }
            $v['option_arr'] =array_values($option_arrse);
            $v['type'] = $type;
            $v['isAnswer']=false;
        }
        $this->success('', $list);
    }

    public function subjectSubmit()
    {
        $param = $this->request->param();
        $user = $this->auth;
        $SubjectModel = new SubjectModel();
        $ExamLog = new ExamLog();

        if($param['type']==1){
            //考试的判断是否超时 或者今天已经考试过了
            if(date('G')>12){
                $this->error('今天考试时间已过');
            }
            $where_find['user_id'] = $user->id;
            $where_find['status']=1;
            $where_find['createtime']=['>',strtotime(date('Y-m-d 00:00:00',time()))];
            $is_exam_log=$ExamLog->where($where_find)->find();
            if(!empty($is_exam_log)){
                $this->error('今天已经考试过了');
            }

        }
        $error_subject=[];
        $true_subject=[];
        foreach ($param['subject'] as $v) {
            $where['id'] = $v['subject_id'];
            $where['answer'] = $v['option'];
            $find = $SubjectModel->where($where)->find();
            if(empty($find)){
                $error_subject[$v['subject_id']]=$v['option'];
            }else{
                $true_subject[$v['subject_id']]=$v['option'];
            }
        }
        $data['user_id']=$user->id;
        $data['createtime']=time();
        $data['status']=$param['type'];
        $data['true_json']=json_encode($true_subject,JSON_UNESCAPED_UNICODE);
        $data['error_json']=json_encode($error_subject,JSON_UNESCAPED_UNICODE);
        $data['all_num']=count($true_subject)+count($error_subject);
        $data['true_num']=count($true_subject);
        $data['score']=(100/$data['all_num'])*count($true_subject);
        $ExamLog->save($data);

        if($param['type']==1){
            $score_num=Config::get("site.examination");
            $memo="完成考试获得积分";
            $type=2;
        }else{
            $score_num=Config::get("site.practice");
            $memo="完成练习获得积分";
            $type=3;
        }
        $score=$this->score_change($user->id,$score_num,'1',$memo,1);
        $this->score_day_log($user->id,$score,0,$type);
        $this->success('', $ExamLog->id);
    }
    public function SubmitResult(){
        $param = $this->request->param();
        $SubjectModel = new SubjectModel();
        $ExamLog = new ExamLog();
        $where['id']=$param['id'];
        $find=$ExamLog->where($where)->find();
        if(empty($find)){
            $this->error('错误');
        }else{
            $error_arr=json_decode($find['error_json'],true);
            $ids=[];
            foreach ($error_arr as $k=>$v){
                $ids[]=$k;
            }
            $where['id'] =['in',$ids];
            $list = $SubjectModel->where($where)->select();
            foreach ($list as &$v) {
                $type = 1;
                $answer = explode(',', $v['answer']);
                if (count($answer) > 1) {
                    $type = 2;
                }
                $option_arr=json_decode($v['option_json'], true);
                foreach ($option_arr as $k=>$vv) {
                    $option_arrse[$k]['option']=$k;
                    $option_arrse[$k]['title']=$vv;
                }
                $v['option_arr'] =array_values($option_arrse);
                $v['type'] = $type;
                $v['my_answer']=$error_arr[$v['id']];
            }
            $find['error_subject']=$list;
            $this->success('',$find);
        }
    }
}
